<?php

namespace App\Model;

use App\Model;
class SchoolModel extends BaseModel
{
    //
    protected $table="school";
    public $timestamps = false;
    protected $guarded = [];

//    关注学校表
    public function keepSchool(){
        return $this -> hasMany(SchoolKeepModel::class,'sid','id');
    }



    public function searchSchool($schoolName,$uuid)
    {

        $res =   SchoolModel::with([
            'keepSchool' =>function($query) use($uuid){
                $query -> where(['uid' => $uuid]) ->get() ? true : false;
            }
        ]) ->where('school_name','like',"%${schoolName}%") ->orderBy('total_card','desc') -> paginate(10);

        if($res){
            return ['code' => true, 'data' => $res ->  makeHidden(['updated_at','create_at'])];
        }else{
            return ['code' => true, 'data' => '查询为空'];
        }

//      $res =   SchoolModel::where('school_name','like',"%${schoolName}%") ->get();
//
//      if($res){
//          return ['code' => true, 'data' => $res];
//      }else{
//          return ['code' => true, 'data' => '查询为空'];
//
//      }

    }

    public function getALLSchool($uuid)
    {
      $res =   SchoolModel::with([
            'keepSchool' =>function($query) use($uuid){
                $query -> where(['uid' => $uuid]) ->get() ? true : false;
            }
        ]) ->orderBy('total_card','desc') -> paginate(10);

      if($res){
          return ['code' => true, 'data' => $res ->  makeHidden(['updated_at','create_at'])];
      }else{
          return ['code' => true, 'data' => '查询为空'];
      }

    }

    public function AddSchool($uuid, $school_name, $address,$image_adar)
    {
        $res =  SchoolModel::create([
            'uid' =>$uuid ,
            'school_name' => $school_name ,
            'address' => $address,
            'head_img' => $image_adar,
            'create_at' => date('Y-m-d H:i:s') ,
        ]);

        if($res){
            return ['code' => true, 'data' => 1];
        }else{
            return ['code' => true, 'data' => 0];
        }
    }


}
